package com.nulldev.util.web.HttpServer3;

import com.nulldev.util.VariableAPI.ArrayUtils;
import com.nulldev.util.VariableAPI.util.strings.splitters.Splitters;

public enum HttpCompressionMethod {
	NONE(null, new String[]
	{ null, "identity" }),

	/* Non-standard */

	/** Technically this is already a WICG standard... */
	ZSTD_NO_DICT(true, "zstd"),
	/* ZSTD_DEFINED_DICT(true, "zstd-d"), */

	SNAPPY(true, "x-snappy-framed"),

	/* Standard */

	GZIP(true, "gzip", new String[]
	{ "gzip", "x-gzip" }),
	DEFLATE(true, "deflate"),
	COMPRESS(true, "compress", new String[]
	{ "compress", "x-compress" }),

	/* LZMA support was removed in 3.0.6 */

	;

	private final String response_value;
	private final String[] val;
	private final boolean quirk_no_length;

	private HttpCompressionMethod(final String response_value, final String[] values) {
		this.quirk_no_length = false;
		this.response_value = response_value;
		this.val = values;
	}

	private HttpCompressionMethod(final boolean quirk_no_length, final String response_value) {
		this(quirk_no_length, response_value, new String[]
			{ response_value });
	}

	private HttpCompressionMethod(final boolean quirk_no_length, final String response_value, final String[] values) {
		this.quirk_no_length = quirk_no_length;
		this.response_value = response_value;
		this.val = values;
	}

	public String getResponseValue() {
		return this.response_value;
	}

	public boolean isValue() {
		return this.val != null;
	}

	public String value() {
		return this.val[0];
	}

	public boolean hasNoLengthQuirk() {
		return this.quirk_no_length;
	}

	public static HttpCompressionMethod parse(final String value) {
		if (value == null || value.length() == 0)
			return NONE;
		final String[] values = Splitters.splitString(value, ", ");
		for (final HttpCompressionMethod method : values()) {
			if (!method.isValue())
				continue;
			if (ArrayUtils.StringArrayUtils.containsString(values, method.value())) {
				return method;
			}
		}
		/**
		 * TODO: https://www.rfc-editor.org/rfc/rfc9110.html#field.content-encoding <br>
		 * tells us that <br>
		 * """ <br>
		 * An origin server MAY respond with a status code of 415 (Unsupported Media
		 * Type) if a representation in the request message has a content coding that is
		 * not acceptable. <br>
		 * """
		 */
		return NONE;
	}
}
